package com.xh.util;


import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

public class JdbcUtil {

    /**
     * @description: 加载驱动
     *
     * @author  wangxihao
     * @email wangxh0108@163.com
    **/
    static {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            System.out.println("驱动加载异常");
        }
    }

    /**
     * @description: 获取连接
     *
     * @author  wangxihao
     * @email wangxh0108@163.com
    **/
    public  static Connection conn(){
        try {
            Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/casemanage?useUnicode=true&characterEncoding=utf-8&useSSL=false", "root", "1214521");
            return connection;
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("数据库连接异常");
        }
        return null;
    }
    /**
     * @description: 设置参数
     *
     * @author  wangxihao
     * @email wangxh0108@163.com
    **/
    public static void setParam(PreparedStatement preparedStatement,Object...ac){
        if(ac != null){
            for (int i = 0; i <ac.length ; i++) {
                try {
                    preparedStatement.setObject(i+1,ac[i]);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return;
    }
    /**
     * @description: 关闭连接
     *
     * @author  wangxihao
     * @email wangxh0108@163.com
    **/
    public static void close(PreparedStatement preparedStatement1,Connection conn1, ResultSet resultSet1){

        try {
            if(preparedStatement1 != null){
                preparedStatement1.close();
            }
            if(conn1 != null){
                conn1.close();
            }
            if(resultSet1 != null){
                resultSet1.close();
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }



    public static int Update(String sql,Object... ac){
        Connection conn = null;
        PreparedStatement prepared = null;
        int j = 0;
        try {
            //获得连接
            conn = conn();
            prepared = conn.prepareStatement(sql);
            //参数赋值
            setParam(prepared,ac);
            j = prepared.executeUpdate();
        }catch (SQLException e) {
            e.printStackTrace();
        }finally {
            close(prepared,conn,null);
        }
        return j;
    }


    public static List<Map> select(String sql, Object...ac){
        List<Map> objects = new ArrayList<>();
        Connection conn = null;
        PreparedStatement preparedStatement = null;
        try {
            conn = conn();
            preparedStatement = conn.prepareStatement(sql);
            setParam(preparedStatement,ac);
            ResultSet resultSet = preparedStatement.executeQuery();
            ResultSetMetaData metaData = resultSet.getMetaData();
            int columnCount = metaData.getColumnCount();
            while (resultSet.next()){
                TreeMap<String, Object> map = new TreeMap<>();

                for (int i = 1; i <= columnCount; i++) {
                    String columnLabel = metaData.getColumnLabel(i);
                    map.put(columnLabel,resultSet.getObject(i));
                }
                objects.add(map);
            }
            close(preparedStatement,conn,resultSet);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return objects;
    }

    public static void main(String[] args){

//        Object[] a = new Object[]{3};
//        FZjdbc01.update("insert sc value(2009,25,?)",a);
//        System.out.println("受影响的行数"+update);


        Object[] objects = {2001};
        System.out.println(JdbcUtil.select("select * from sc where s_no = ?", objects));


    }
}
